#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include"data_struct.h"
#include"args.h"
/**
 * 打印总还款信息
 */
void print_total_pay_info(total_info_t * info){
    printf("总贷款额:%.2f元\n",info->total_money); 
    printf("总贷款期数:%d期\n",info->total_months);
    printf("固定年利率:%.2f%%\n",info->year_rate*100);
    printf("每月固定还款额度:%.2f元\n",info->money_per_month);
    printf("总利息:%.2f元\n",info->total_interest);
    printf("总还款额:%.2f元\n",info->total_pay);
    
    for(int i=0;i<info->total_months;i++){
        struct per_month_pay_info * one = info->vec+i;
        printf("第%d月:本金:%.2f,利息:%.2f,剩余本金:%.2f,剩余利息:%.2f,\n",i+1,one->capital,one->interest,one->capital_future_pay,one->interest_future_pay);
    }
    printf("------------------------------------------------------\n");
}

void print_one_month(total_info_t * info, int month){
    printf("总贷款额:%.2f元\n",info->total_money); 
    printf("总贷款期数:%d期\n",info->total_months);
    printf("固定年利率:%.2f%%\n",info->year_rate*100);
    printf("每月固定还款额度:%.2f元\n",info->money_per_month);
    printf("总利息:%.2f元\n",info->total_interest);
    printf("总还款额:%.2f元\n",info->total_pay);
    per_info_t * one = info->vec+month-1;
    printf("第%d月:本金:%.2f,利息:%.2f,剩余本金:%.2f,剩余利息:%.2f,\n",month,one->capital,one->interest,one->capital_future_pay,one->interest_future_pay);

}

void init_total_pay_info(total_info_t* info, double total_money,int total_months,double year_rate){
    info->total_money =total_money;
    info->total_months = total_months;
    info->year_rate = year_rate;
    info->month_rate = year_rate/12;
    double temp = pow(1+info->month_rate,info->total_months);
    info->money_per_month = info->total_money*info->month_rate*temp/(temp-1);
    info->total_pay = info->money_per_month * info->total_months;
    info->total_interest = info->total_pay-info->total_money;
    info->vec = malloc(sizeof(per_info_t)*info->total_months);

    double  _temp_total_money = info->total_money;
    double  _temp_total_interest = info->total_interest;
    for(int m=0;m<info->total_months;m++){
        //当月利息
        info->vec[m].interest = _temp_total_money*info->month_rate;
        //当月剩余利息
        _temp_total_interest -= info->vec[m].interest;
        info->vec[m].interest_future_pay = _temp_total_interest;
        //累计已还利息
        info->vec[m].interest_already_pay = info->total_interest-_temp_total_interest;


        //当月本金
        info->vec[m].capital = info->money_per_month-info->vec[m].interest;
        //剩余本金
        _temp_total_money -=info->vec[m].capital;
        //当月剩余本金
        info->vec[m].capital_future_pay = _temp_total_money;
        //累计已还本金
        info->vec[m].capital_already_pay = info->total_money-_temp_total_money;
    }
}

total_info_t*  shift_pay(total_info_t* info, int event_month, double forward_money){
    per_info_t *pinfo = info->vec+event_month-1;
    double total_money = pinfo->capital_future_pay-forward_money;
    int total_months = info->total_months - event_month;
    total_info_t * new_info = malloc(sizeof(total_info_t));
    init_total_pay_info(new_info,total_money,total_months,info->year_rate);
    return new_info;
}
